﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_Agent")]
    public class T_Agent : IEntity<T_Agent>
    {
        #region 
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int PeriodicalId { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string UserName { get; set; }

        public DateTime RecordDate { get; set; }

        public DateTime RefreshDate { get; set; }

        /// <summary>
        /// 影响排序 越高越靠前
        /// </summary>
        public int Rank { get; set; }
        #endregion


        public override T_Agent Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Agent Find(System.Linq.Expressions.Expression<Func<T_Agent, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Agent.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.PeriodicalId = item.PeriodicalId;
                    this.RecordDate = item.RecordDate;
                    this.RefreshDate = item.RefreshDate;
                    this.UserName = item.UserName;
                    this.Rank = item.Rank;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Agent, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Agent.Any(where);
            }
        }
    }
}
